/*
 * PLCEditor.java
 */
package plc;

import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
//import plc.language.Lexer;
//import plc.language.parser;
//import plc.language.processors.*;

/**
 * The application's main frame.
 */
public class PLCEditor extends FrameView {
    private final PLCController controller;

    public PLCEditor(SingleFrameApplication app, PLCController control) {
        super(app);
        initComponents();
        this.controller = control;
        txtEditor.setText("4 Baht per watt_hour\n"
                + "device 1 = light1 30 watt\n"
                + "device 2 = light2 35 watt\n"
                + "device 3 = light3 40 watt\n"
                + "device 4 = Sony52 285 watt\n"
                + "device 5 = PanasonicTH50 163 watt\n"
                + "device 6 = SamsungPN50 207 watt \n"
                + "device 7 = PCi7HiEnd 492 watt \n"
                + "device 8 = Microwave 1000 watt \n"
                + "device 9 = SandwichToaster 900 watt \n"

                + "function switchAllLights { turn on light1; turn on light2; turn on light3; }\n"
                + "function switchOffAllLights { turn off light1; turn off light2; turn off light3; }\n"
                + "function switchPC { if ( PCi7HiEnd is on ) then { turn off PCi7HiEnd; } else { turn on PCi7HiEnd; }}\n"
                + "function switchOffTV { turn off PanasonicTH50; turn off Sony52; turn off SamsungPN50; }\n"
//                + "function switchSamsung { if ( SamsungPN50 is on ) then { turn off SamsungPN50; } else { turn on SamsungPN50; }}\n"
                + "button TV_ON = turn on PanasonicTH50;\n"
                + "button Light_On = call_func switchAllLights;\n"
                + "button Light_Off = call_func switchOffAllLights;\n"
                + "button PC_ON_OFF = call_func switchPC;\n"
                + "button TURN_ON_MICROWAVE = turn on Microwave;\n"
                + "button TURN_OFF_MICROWAVE = turn off Microwave;\n"
                + "button TURN_ON_TOASTER = turn on SandwichToaster;\n"
                + "button TURN_OFF_TOASTER = turn off SandwichToaster;\n"
                + "button CHECK_WATT = if ( total_watt > 10 and Sony52 is on) then { call_func switchOffTV; }\n"
                + "timer 1 = turn on light1 at 11:00 everyday\n"
//                + "timer 2 = turn off television at 20:00 every Monday\n"
//                + "timer 3 = turn on television2 at 1:00 on Monday Tuesday\n"
                + "turn on Sony52;\n"
                + "call_func switchAllLights;"
                );

    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = PLCApp.getApplication().getMainFrame();
            aboutBox = new PLCAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        PLCApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        editorPane = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtEditor = new javax.swing.JEditorPane();
        outputPane = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtOutput = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        btnPane = new javax.swing.JPanel();
        btnCompile = new javax.swing.JButton();
        btnRun = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem menuExit = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        editorPane.setName("editorPane"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(PLCEditor.class);
        txtEditor.setText(resourceMap.getString("txtEditor.text")); // NOI18N
        txtEditor.setName("txtEditor"); // NOI18N
        txtEditor.setPreferredSize(new java.awt.Dimension(750, 400));
        jScrollPane3.setViewportView(txtEditor);

        javax.swing.GroupLayout editorPaneLayout = new javax.swing.GroupLayout(editorPane);
        editorPane.setLayout(editorPaneLayout);
        editorPaneLayout.setHorizontalGroup(
            editorPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 922, Short.MAX_VALUE)
            .addGroup(editorPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(editorPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        editorPaneLayout.setVerticalGroup(
            editorPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 429, Short.MAX_VALUE)
            .addGroup(editorPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(editorPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        outputPane.setName("outputPane"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtOutput.setColumns(20);
        txtOutput.setEditable(false);
        txtOutput.setRows(5);
        txtOutput.setName("txtOutput"); // NOI18N
        jScrollPane1.setViewportView(txtOutput);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout outputPaneLayout = new javax.swing.GroupLayout(outputPane);
        outputPane.setLayout(outputPaneLayout);
        outputPaneLayout.setHorizontalGroup(
            outputPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputPaneLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addContainerGap(742, Short.MAX_VALUE))
            .addGroup(outputPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 776, Short.MAX_VALUE))
        );
        outputPaneLayout.setVerticalGroup(
            outputPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputPaneLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addContainerGap(187, Short.MAX_VALUE))
            .addGroup(outputPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(outputPaneLayout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
        );

        btnPane.setName("btnPane"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(PLCEditor.class, this);
        btnCompile.setAction(actionMap.get("compile")); // NOI18N
        btnCompile.setText(resourceMap.getString("btnCompile.text")); // NOI18N
        btnCompile.setName("btnCompile"); // NOI18N

        btnRun.setAction(actionMap.get("run")); // NOI18N
        btnRun.setText(resourceMap.getString("btnRun.text")); // NOI18N
        btnRun.setName("btnRun"); // NOI18N

        javax.swing.GroupLayout btnPaneLayout = new javax.swing.GroupLayout(btnPane);
        btnPane.setLayout(btnPaneLayout);
        btnPaneLayout.setHorizontalGroup(
            btnPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(btnPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(btnPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRun, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                    .addComponent(btnCompile, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
        );
        btnPaneLayout.setVerticalGroup(
            btnPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, btnPaneLayout.createSequentialGroup()
                .addComponent(btnCompile, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRun, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(editorPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(outputPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(editorPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(outputPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(btnPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(87, 87, 87))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        menuExit.setAction(actionMap.get("quit")); // NOI18N
        menuExit.setName("menuExit"); // NOI18N
        fileMenu.add(menuExit);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void compile() {
        System.out.println("Editor: compile");

        // clear output console
        txtOutput.setText("");

        String language = txtEditor.getText();
        controller.parse(language);
        
    }

    @Action
    public void run() {
        controller.run();
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCompile;
    private javax.swing.JPanel btnPane;
    private javax.swing.JButton btnRun;
    private javax.swing.JPanel editorPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel outputPane;
    private javax.swing.JEditorPane txtEditor;
    private javax.swing.JTextArea txtOutput;
    // End of variables declaration//GEN-END:variables
    private JDialog aboutBox;

    void output(String message) {
        txtOutput.append(message + "\n");
    }

    /**
     * heightlight code compile error
     * @param line 
     */
    void setErrorLine(int line) {
        String txt = txtEditor.getText();
        
        // find line position
        int start = 0;
        for (int i = 0; i < line - 1; i++) {
            start = txt.indexOf("\n", start);
            System.out.println("caret position: " + start);
            txtEditor.requestFocus();
            txtEditor.setCaretPosition(start);
        }
    }
}
